package com.fare.taluo.core.domain.system;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Where;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.base.ddd.domain.baseentity.BaseEntity;

@javax.persistence.Entity
@Table(name = "VERSION_INFO")
@Where(clause = "flg_deleted=0")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class VersionInfoEntity extends BaseEntity{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5110808620592173857L;
	@Column(name = "VERSION_NUMBER")
	private String versionNumber;
	@Column(name = "PUBLISHER")
	private String publisher;
	@Column(name = "PUBLISH_TIME")
	@Temporal(TemporalType.TIMESTAMP)
	private Date publishTime;
	@Column(name = "CREATER")
	private Long creater;
	@Column(name = "CREATE_TIME")
	private Date createTime = new Date();
	@Column(name = "FLG_DELETED")
	private Integer flgDeleted = 0;

	/**
	 * 子舱位
	 */
	@Cascade({org.hibernate.annotations.CascadeType.ALL})
	@OneToMany(orphanRemoval=true, fetch = FetchType.LAZY, mappedBy = "versionInfo")
	@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
	@OrderBy(value="seq_Num, createTime desc")
	private Set<VersionDetailInfoEntity> details = new HashSet<VersionDetailInfoEntity>();
	
	protected VersionInfoEntityReposity getVersionInfoReposity() {
		return InstanceFactory.getInstance(VersionInfoEntityReposity.class);
	}

	// 获取版本更新信息（首页）
	public List<VersionInfoEntity> findVersionInfos(Long versionId) {
		return getVersionInfoReposity().findVersionInfo(versionId);
	}

	// 获取版本信息
	public List<VersionInfoEntity> findAllEntities() {
		return getVersionInfoReposity().findAllEntities();
	}

	// 增加/修改信息
	public void saveOrUpdate(VersionInfoEntity versionInfoEntity) {
		getVersionInfoReposity().saveOrUpdate(versionInfoEntity);
	}

	// 获取刚添加的版本信息ID作为插入版本详细信息时用
	public VersionInfoEntity getMaxVersion() {
		return getVersionInfoReposity().getMaxVersion();
	}


	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Long getCreater() {
		return creater;
	}

	public void setCreater(Long creater) {
		this.creater = creater;
	}

	public Integer getFlgDeleted() {
		return flgDeleted;
	}

	public void setFlgDeleted(Integer flgDeleted) {
		this.flgDeleted = flgDeleted;
	}

	
	public Set<VersionDetailInfoEntity> getDetails() {
		return details;
	}

	public void setDetails(Set<VersionDetailInfoEntity> details) {
		this.details = details;
	}

	public String getVersionNumber() {
		return versionNumber;
	}

	public void setVersionNumber(String versionNumber) {
		this.versionNumber = versionNumber;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public Date getPublishTime() {
		return publishTime;
	}

	public void setPublishTime(Date publishTime) {
		this.publishTime = publishTime;
	}

	@Override
	public String toString() {
		return "VersionInfoEntity [versionNumber=" + versionNumber
				+ ", publisher=" + publisher + ", publishTime=" + publishTime
				+ ", creater=" + creater + ", createTime=" + createTime
				+ ", flgDeleted=" + flgDeleted + "]";
	}
	
	
}
